﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using System.IO;
using System.Text.RegularExpressions;

/// <summary>
/// 用于在子App中，将主App的代码和资源复制到子App中
/// </summary>
public class CopyAndUpdateMainApp : MonoBehaviour {

    private static string _MainAppFolder = "/../bsgmainapp-unity-lua/";

    private static string[] _dirs = new string[]{ 
        "Assets/Spine/","Assets/Sqlitekit/","LuaEncoder/",
        "Assets/Lua/Comm/","Assets/LuaFramework/",
		"Assets/Plugins/Android/","Assets/Plugins/iOS/","Assets/Assets/Plugins/x86/","Assets/Plugins/x86_64/","Assets/Plugins/tolua.bundle",
        "Assets/StreamingAssets/mainapp","Assets/StreamingAssets/mainappraw"
    };

    private static string[] _files = new string[]
    { 
		"Assets/Plugins/CString.dll","Assets/Plugins/Debugger.dll",
        "ProjectSettings/TagManager.asset","ProjectSettings/TimeManager.asset",
        "ProjectSettings/Physics2DSettings.asset"
    };

    [MenuItem("LuaFramework/Copy And Update MainApp", false, 250)]
    static void Execute(){
        //MainApp路径
        string currProjPath = Application.dataPath.Substring(0,Application.dataPath.Length-7);
      
        int count = Regex.Matches(_MainAppFolder,@"(\..)").Count;
        string src = currProjPath;
        DirectoryInfo dirInfo = new DirectoryInfo(src);
        for (int i = 0; i < count; ++i)
        {
            dirInfo = dirInfo.Parent;
        }
        src = dirInfo.FullName + _MainAppFolder.Replace("../","");

        //复制文件夹
        foreach (string dir in _dirs)
        {
            string srcFolder = src + "/" + dir;
            string destFolder = currProjPath + "/" + dir;
            if (Directory.Exists(srcFolder))
            {
                if (Directory.Exists(destFolder))
                {
                    Directory.Delete(destFolder,true);
                }
                Directory.CreateDirectory(destFolder);
                CopyDirectory(srcFolder,destFolder,true);
            }

            if (File.Exists(srcFolder + ".meta"))
            {
                File.Copy(srcFolder + ".meta",destFolder + ".meta",true);
            }
        }

        //复制文件
        foreach (string file in _files)
        {
            string srcFolder = src + "/" + Path.GetDirectoryName(file) + "/";
            string destFolder = currProjPath + "/" + Path.GetDirectoryName(file)+ "/";
            if (!Directory.Exists(Path.GetDirectoryName(destFolder)))
            {
                Directory.CreateDirectory(destFolder);
            }

			string fileUrl = srcFolder + Path.GetFileName(file);
            if (File.Exists(fileUrl))
            {
				Debug.LogError(fileUrl + " >> " +currProjPath  + "/" + file );
				File.Copy(fileUrl,currProjPath  + "/" + file,true);
            }
            if (File.Exists(fileUrl+ ".meta"))
            {
				File.Copy(fileUrl + ".meta", currProjPath + "/" + file + ".meta",true);
            }
        }
        AssetDatabase.Refresh();
    }



	[MenuItem("LuaFramework/Copy Export To Test", false, 251)]
	static void Execute1(){
		//MainApp路径
		string currProjPath = Application.dataPath.Substring(0,Application.dataPath.Length-7);

		int count = Regex.Matches(_MainAppFolder,@"(\..)").Count;
		string dest = currProjPath;
		DirectoryInfo dirInfo = new DirectoryInfo(dest);
		for (int i = 0; i < count; ++i)
		{
			dirInfo = dirInfo.Parent;
		}
		dest = dirInfo.FullName + _MainAppFolder.Replace("../","");

		string destFolder = dest + "/MainApp.app/Contents/Resources/Data/StreamingAssets";
		string srcFolder = Application.streamingAssetsPath;
		if (Directory.Exists(destFolder) && Directory.Exists(srcFolder))
		{
			if (Directory.Exists(destFolder))
			{
				Directory.Delete(destFolder,true);
			}
			Directory.CreateDirectory(destFolder);
			CopyDirectory(srcFolder,destFolder,true);
		}
	}

    static void CopyDirectory(string sourceDirName, string destDirName, bool copySubDirs)
    {
        // Get the subdirectories for the specified directory.
        DirectoryInfo dir = new DirectoryInfo(sourceDirName);

        if (!dir.Exists)
        {
            throw new DirectoryNotFoundException(
                "Source directory does not exist or could not be found: "
                + sourceDirName);
        }

        DirectoryInfo[] dirs = dir.GetDirectories();
        // If the destination directory doesn't exist, create it.
        if (!Directory.Exists(destDirName))
        {
            Directory.CreateDirectory(destDirName);
        }

        // Get the files in the directory and copy them to the new location.
        FileInfo[] files = dir.GetFiles();
        foreach (FileInfo file in files)
        {
            string temppath = Path.Combine(destDirName, file.Name);
            file.CopyTo(temppath, true);
        }

        // If copying subdirectories, copy them and their contents to new location.
        if (copySubDirs)
        {
            foreach (DirectoryInfo subdir in dirs)
            {
                string temppath = Path.Combine(destDirName, subdir.Name);
                CopyDirectory(subdir.FullName, temppath, copySubDirs);
            }
        }
    }
}
